package com.scalar.chiptrack.operations.finishedGoods.actionCommands;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.operations.finishedGoods.forms.FinishedGoodsEntryForm;
import com.scalar.chiptrack.operations.finishedGoods.EquivalentPartNumber;
import com.scalar.chiptrack.operations.finishedGoods.FinishedGoodsHelper;
import com.scalar.chiptrack.operations.finishedGoods.dao.FinishedGoodsDAO;
import com.scalar.chiptrack.operations.processtypesetup.dao.ProcessTypeSetupDAO;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * User: Murali Krishna Yengala
 * Date: Jun 1, 2005
 * Time: 3:09:55 PM
 */
public class FinishedGoodsEditCommand extends ChipTrackExtendedAction{

    public ActionForward executeRequest( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception
    {
        FinishedGoodsEntryForm finishedGoodsEntryForm = ( FinishedGoodsEntryForm ) form;
        HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );

        int processStepIdI = finishedGoodsEntryForm.getProcessStepIdI();
        int baseProductIdI = finishedGoodsEntryForm.getBaseProductIdI();
        ArrayList equivalentPartList = finishedGoodsEntryForm.getEquivalentPartNosList();
        if( equivalentPartList == null || equivalentPartList.size() == 0)
        {
            equivalentPartList = new ArrayList();
            equivalentPartList.add(new EquivalentPartNumber());
            finishedGoodsEntryForm.setEquivalentPartNosList( equivalentPartList);
        }
        FinishedGoodsHelper.setFgiProducts( baseProductIdI, request, errors, info);
        FinishedGoodsHelper.setCustomers( request, errors );
        finishedGoodsEntryForm.setVendorList( FinishedGoodsHelper.getVendorLocations( request));
        request.setAttribute( "units", ProcessTypeSetupDAO.getUnitsOfMesurment() );
        request.setAttribute( "algorithmList", FinishedGoodsDAO.loadAlgorithmList());
        if( processStepIdI == -1)
        {
            return  mapping.findForward("newEntry");
        }
        else
        {
            return  mapping.findForward("editFg");
        }
    }
}
